// 版本管理
app.controller('version', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', function ($scope, $rootScope, commonFactory, toaster, $modal) {
    $scope.versionData = {
        Index: 1,
        Size: 10
    }
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.versionData.Index >= $scope.page) {//判断最大页码
                $scope.versionData.Index = $scope.page;
                return false;
            }
            $scope.versionData.Index++
            requestVersion();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.versionData.Index <= 1) {//判断最小页码
            $scope.versionData.Index = 1;
            return false;
        }
        $scope.versionData.Index--;
        requestVersion()
    }
    // 添加
    $scope.addVersion = function () {
        localStorage.setItem("versiondata", '');
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                $scope.versionItems = {
                    Code: '',
                    Content: '',
                    IsDialog: 'false'
                }
                $scope.isadd = true;
                $scope.istrue = false;
                $scope.addversion = function () {
                    if (checkinp() && $scope.isadd) {
                        $scope.isadd = false
                        commonFactory.ajax("/api/version/Save", $scope.versionItems, function (result) {
                            if (result.Code == 1) {
                                $scope.isadd = true;
                                toaster.pop('success', '', '添加版本成功');
                                $modalInstance.close('cancel');
                            } else {
                                toaster.pop('warning', '', result.Data||'添加版本失败');
                                $modalInstance.close('cancel');
                            }
                        }, function () {
                            toaster.pop('error', '', '后台错误');
                        }, "post");
                    }
                }
                var checkinp = function () {
                    if (!$scope.versionItems.Code) {
                        toaster.pop('warning', '', '请填写版本号');
                        return
                    }
                    if (!$scope.versionItems.Content) {
                        toaster.pop('warning', '', '请填写更新内容');
                        return
                    }
                    var vcont = document.getElementById("vcont")
                    if(vcont){
                        vcont.scrollTop =  vcont.scrollHeight;
                        if(vcont.scrollTop>0){
                            toaster.pop('warning', '', '更新内容最大可输入四行');
                            return
                        } 
                    }
                    return true
                }

                $scope.cancel = function () {
                    $modalInstance.close('cancel');
                };
            }],
            size: '',
            backdrop: true
        });
        modalInstance.result.then(function (selectedItem) {
            requestVersion()
        }, function () {

        });
    }
    // 编辑
    $scope.editVersion = function (pid) {
        commonFactory.ajax("/api/version/GetDataById", {'id':pid}, function (result) {
            if (result.Code == 1) {
                var modalInstance = $modal.open({
                    templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                    controller: ['$scope', '$modalInstance', 'toaster', 'items', 'commonFactory', '$timeout', '$interval', function ($scope, $modalInstance, toaster, items, commonFactory, $timeout, $interval) {
                        items.IsDialog = items.IsDialog?'true':'false';
                        $scope.versionItems = items;
                        $scope.isadd = true;
                        $scope.istrue = true;
                        $scope.editversion = function () {
                            if (checkinp() && $scope.isadd) {
                                $scope.isadd = false
                                commonFactory.ajax("/api/version/Update", $scope.versionItems, function (result) {
                                    if (result.Code == 1) {
                                        $scope.isadd = true;
                                        toaster.pop('success', '', '编辑版本成功');
                                        $modalInstance.close('cancel');
                                    } else {
                                        toaster.pop('warning', '', result.Data||'添加版本失败');
                                        $modalInstance.close('cancel');
                                    }
                                }, function () {
                                    toaster.pop('error', '', '后台错误');
                                }, "post");
                            }
                        }
                        var checkinp = function () {
                            if (!$scope.versionItems.Code) {
                                toaster.pop('warning', '', '请填写版本号');
                                return
                            }
                            if (!$scope.versionItems.Content) {
                                toaster.pop('warning', '', '请填写更新内容');
                                return
                            }
                            var vcont = document.getElementById("vcont")
                            if(vcont){
                                vcont.scrollTop =  vcont.scrollHeight;
                                if(vcont.scrollTop>0){
                                    toaster.pop('warning', '', '更新内容最大可输入四行');
                                    return
                                } 
                            }
                            return true
                        }

                        $scope.cancel = function () {
                            $modalInstance.close('cancel');
                        };
                    }],
                    size: '',
                    backdrop: false,
                    resolve: {
                        items: function () {
                            return result.Data;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    requestVersion()
                }, function () {

                });
            } else {
                toaster.pop('warning', '', '编辑版本失败');
                $modalInstance.close('cancel');
            }
        }, function () {
            toaster.pop('error', '', '后台错误');
        }, "get");
    }
    // 发布
    $scope.ReleaseVersion = function (pid) {
        console.log(pid)
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', 'items', function ($scope, $modalInstance, items) {
                $scope.ReleaseVersion = items;
                $scope.ok = function () {
                    commonFactory.loading(true)
                    commonFactory.ajax("/api/version/Publish", { 'Id': $scope.ReleaseVersion }, function (result) {
                        commonFactory.loading(false)
                        if (result.Code == 1) {
                            toaster.pop('success', '成功', '发布成功');
                            $modalInstance.close();
                        } else {
                            toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                            $modalInstance.close();
                        }
                    }, function () {
                        toaster.pop('error', '失败', '后台未返回报错');
                    }, "get");
                }
                $scope.cancel = function () {
                    $modalInstance.close();
                };
            }
            ],
            size: 'sm',
            backdrop: false,
            resolve: {
                items: function () {
                    return pid;
                }
            }
        });
        modalInstance.result.then(function () {
            requestVersion();
        }, function () {
        });
    }
    // 删除
    $scope.deleteVersion = function (pid) {
        console.log(pid)
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', 'items', 'toaster', 'commonFactory', function ($scope, $modalInstance, items, toaster, commonFactory) {
                $scope.deleteVersion = items;
                $scope.ok = function () {
                    //删除
                    commonFactory.loading(true)
                    commonFactory.ajax("/api/version/OperaDelete", { 'Id': $scope.deleteVersion }, function (result) {
                        commonFactory.loading(false)
                        if (result.Code == 1) {
                            toaster.pop('success', '成功', '删除成功');
                            $modalInstance.close();
                        } else {
                            toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                            $modalInstance.close();
                        }
                    }, function () {
                        toaster.pop('error', '失败', '后台未返回报错');
                    }, "get");
                }

                $scope.cancel = function () {
                    $modalInstance.close('cancel');
                };
            }],
            size: 'sm',
            backdrop: false,
            resolve: {
                items: function () {
                    return pid;
                }
            }
        });
        modalInstance.result.then(function () {
            requestVersion();
        }, function () {

        });
    };
    var requestVersion = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/version/QueryList", $scope.versionData, function (result) {
            if(result.Code==1){
                if(result.Data.List.length==0){
                    $scope.permitAdd = true;
                }else{
                    for (var i = result.Data.List.length - 1; i >= 0; i--) {
                        if(!result.Data.List[i].IsPublish){
                            $scope.permitAdd = false;
                            break;
                        }
                        $scope.permitAdd = true;
                    } 
                }
                commonFactory.loading(false)
                $scope.graybtn = false;
                $scope.versionList = result.Data.List;
                $scope.QueryListTotal = result.Data.Total;
                $scope.page = Math.ceil(result.Data.Total / $scope.versionData.Size);
                
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    requestVersion()
}]);
// 渠道统计管理
app.controller('channel', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', '$timeout', function ($scope, $rootScope, commonFactory, toaster, $modal, $timeout) {
    $scope.channelData = {
        pageIndex: 1,
        pageSize: 10,
        title:'',
        description:''
    }
     //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.channelData.pageIndex >= $scope.page) {//判断最大页码
                $scope.channelData.pageIndex = $scope.page;
                return false;
            }
            $scope.channelData.pageIndex++
            requestchannelList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.channelData.pageIndex <= 1) {//判断最小页码
            $scope.channelData.pageIndex = 1;
            return false;
        }
        $scope.channelData.pageIndex--;
        requestchannelList()
    }
    var requestchannelList = function () {
        commonFactory.loading(true)  
        commonFactory.ajax("/api/SourceType/GetSourceTypeList", $scope.channelData, function (result) {
            commonFactory.loading(false)  
            $scope.graybtn = false;
            if (result.Code == 1) {
                $scope.channelList = result.List;
                $scope.channelTotal = result.Total;
                $scope.page = Math.ceil(result.Total / $scope.channelData.pageSize);
            }
        }, function () {
            $scope.graybtn = false;
        }, "post");
    }
    requestchannelList()
    // 查询
    $scope.querychannel = function () {
        $scope.channelData.pageIndex = 1;
        $scope.graybtn = true;
        requestchannelList();
    }
    // 添加
    $scope.addchannel = function () {
        localStorage.setItem("channel", '');
        var modalInstance = $modal.open({
            templateUrl:'tpl/model/delPmodel.html?v='+Math.random(),
            controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                $scope.channelshow  = true;
                $scope.channelitems = {
                    Name:'',
                    Description: ''
                }
                $scope.isadd=true;
                var checkinp = function () {
                    if (!$scope.channelitems.Name) {
                        toaster.pop('warning', '', '请填写渠道名称');
                        return
                    }
                    if ($scope.channelitems.Description==="") {
                        toaster.pop('warning', '', '请填写渠道描述');
                        return
                    }
                    return true
                }
                $scope.istrue = false;
                $scope.ok = function () {
                    if (checkinp()&&$scope.isadd) {
                        $scope.isadd=false
                        localStorage.setItem("channel",'');
                        commonFactory.loading(true)  
                        commonFactory.ajax("/api/SourceType/Save", $scope.channelitems, function (result) {
                            commonFactory.loading(false)  
                            if (result.Code == 1) {
                                $scope.isadd=true;
                                toaster.pop('success', '', '添加渠道成功');
                                $modalInstance.close('cancel');
                            } else {
                                toaster.pop('warning', '', '添加渠道失败');
                                $modalInstance.close('cancel');
                            }
                        }, function () {
                            toaster.pop('error', '', '后台错误');
                        }, "post");
                    }
                }
                
                $scope.cancel = function () {
                    $modalInstance.close('cancel');
                };
            }],
            size: '',
            backdrop:false
        });
        modalInstance.result.then(function (selectedItem) {
            requestchannelList()
        }, function () {

        });
    }
    // 编辑
    $scope.editchannel = function (cId) {
        var modalInstance = $modal.open({
            templateUrl:'tpl/model/delPmodel.html?v='+Math.random(),
            controller: ['$scope', '$modalInstance', 'toaster', 'commonFactory', '$timeout', function ($scope, $modalInstance, toaster, commonFactory, $timeout) {
                $scope.channelshow  = true;
                $scope.channelitems = {
                    Id: cId,
                    Name:'',
                    Description: ''
                }
                $scope.isadd=true;
                var checkinp = function () {
                    if (!$scope.channelitems.Name) {
                        toaster.pop('warning', '', '请填写渠道名称');
                        return
                    }
                    if ($scope.channelitems.Description==="") {
                        toaster.pop('warning', '', '请填写渠道描述');
                        return
                    }
                    return true
                }
                var initDate = function () {
                    $scope.Id = localStorage.getItem("NoticesId");
                    commonFactory.ajax("/api/SourceType/GetSourceType", { "": cId }, function (result) {
                        $scope.channelitems = result
                    }, function () {
                        toaster.pop('error', '', '后台错误');
                    }, "post");
                }
                initDate()
                $scope.ok = function () {
                    if (checkinp()) {
                        commonFactory.loading(true)  
                        commonFactory.ajax("/api/SourceType/Update", $scope.channelitems, function (result) {
                            commonFactory.loading(false)  
                            if (result.Code == 1) {
                                toaster.pop('success', '', '编辑成功');
                                $modalInstance.close('cancel');
                                
                            } else {
                                toaster.pop('warning', '', '编辑失败');
                                $modalInstance.close('cancel');
                            }
                        }, function () {
                            toaster.pop('error', '', '后台错误');
                        }, "post");
                    }
                }
                $scope.cancel = function () {
                    $modalInstance.close('cancel');
                };
            }],
            size: '',
            backdrop:false
        });
        modalInstance.result.then(function (selectedItem) {
            requestchannelList()
        }, function () {

        });
    }    
}]);
// 清除指定缓存
app.controller('clearappointedcache', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', function ($scope, $rootScope, commonFactory, toaster, $modal) {
    $scope.cacheData = {
        cache: ''
    }
    $scope.queryCacheData = {
        key: ''
    }
    $scope.clearappointedcache = function(){
        if ($scope.cacheData.cache=="") {
            toaster.pop('warning','提示','请输入缓存参数')
            return;
        }
        var uId = localStorage.getItem('u_id');
        commonFactory.loading(true) 
        commonFactory.ajax(commonFactory.baseurl + "/api/common/delkey?key=" + $scope.cacheData.cache, { u_id: uId }, function (result) {
            commonFactory.loading(false) 
            toaster.pop('success','成功','成功删除缓存')
        },function(err){
            commonFactory.loading(false) 
            toaster.pop('error','失败','删除缓存失败')
        })
    }
    $scope.QueryCache = function(){
        if ($scope.queryCacheData.key=="") {
            toaster.pop('warning','提示','请输入要查询缓存')
            return;
        }
        var uId = localStorage.getItem('u_id');
        commonFactory.loading(true) 
        commonFactory.ajax(commonFactory.baseurl + "/api/Common/GetKey?key=" + $scope.queryCacheData.key, { u_id: uId }, function (result) {
            commonFactory.loading(false) 
            $scope.cacheresult = result
            toaster.pop('success','成功','缓存查询成功')
        },function(err){
            commonFactory.loading(false) 
            toaster.pop('error','失败','缓存查询失败')
        })
    } 
}]);
// 修改可提现金额(现修改系统数据)
app.controller('editprice', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', function ($scope, $rootScope, commonFactory, toaster, $modal) {
    $scope.unbindData = {
        userId: ''
    }
    $scope.unbindcard = function(){
        if ($scope.unbindData.userId=="") {
            toaster.pop('warning','提示','请输入用户Id')
            return;
        }
        commonFactory.loading(true)  
        commonFactory.ajax("/api/User/CancelLocalBankCard", $scope.unbindData, function (result) {
            commonFactory.loading(false)  
            if(result.Code == 1){
                toaster.pop('success','成功',result.Data)
            }else{
                toaster.pop('error','失败',result.Data)
            }
        }, function () {

        }, "post");
    }
    $scope.editMobileData = {
        oldMobile:'',
        newMobile:''
    }
    $scope.editPData = {
        userId: '',
        usablePrice: ''
    }
    $scope.editprice = function(){
        if ($scope.editPData.userId=="") {
            toaster.pop('warning','提示','请输入用户Id')
            return;
        }
        if ($scope.editPData.usablePrice=="") {
            toaster.pop('warning','提示','请输入可提现金额')
            return;
        }
        commonFactory.loading(true)  
        commonFactory.ajax("/api/User/EditUsablePrice", $scope.editPData, function (result) {
            commonFactory.loading(false)  
            if(result.Code == 1){
                toaster.pop('success','成功',result.Data)
            }else{
                toaster.pop('error','失败',result.Data)
            }
        }, function () {

        }, "post");
    }
    $scope.editMobileData = {
        oldMobile:'',
        newMobile:''
    }
    $scope.editmobile = function(){
        if (!$scope.editMobileData.oldMobile) {
            toaster.pop('warning', '提示', '请输入原手机号');
            return;
        }
        if (!$scope.editMobileData.newMobile) {
            toaster.pop('warning', '提示', '请输入现手机号');
            return;
        }
        commonFactory.loading(true)  
        commonFactory.ajax("/api/Common/ChangeMobile?oldMobile="+$scope.editMobileData.oldMobile+"&newMobile="+$scope.editMobileData.newMobile, {}, function (result) {
            commonFactory.loading(false)  
            if(result.Code == 1){
                toaster.pop('success','成功','修改手机号成功')
            }else{
                toaster.pop('error','失败','修改手机号失败')
            }
        }, function () {

        }, "post");
    }
}]);
// // 修改预留手机号
// app.controller('changecard', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', function ($scope, $rootScope, commonFactory, toaster, $modal) {
//     $scope.changeData = {
//         oldMobile: '',
//         newMobile: ''
//     }
//     $scope.changecard = function(){
//         if ($scope.changeData.oldMobile=="") {
//             toaster.pop('warning','提示','请输入旧手机号')
//             return;
//         }
//         if ($scope.changeData.newMobile=="") {
//             toaster.pop('warning','提示','请输入新手机号')
//             return;
//         }
//         commonFactory.loading(true)        
//         commonFactory.ajax("/api/User/UpdateUserBankMobile", $scope.changeData, function (result) {
//             commonFactory.loading(false)            
//             if(result.Code == 1){
//                 toaster.pop('success','成功',result.ResposeData)
//             }else{
//                 toaster.pop('error','失败',result.ResposeData)
//             }
//         }, function () {

//         }, "post");
//     }   
// }]);
// // 银行接口管理
// app.controller('cancelst', ['$scope', '$rootScope', 'commonFactory', 'toaster', '$modal', function ($scope, $rootScope, commonFactory, toaster, $modal) {
//     $scope.cancelstData = {
//         contract: '',
//         contract1:''
//     }
//     // 取消授权
//     $scope.CancelAuthorization = function(){
//         if ($scope.cancelstData.contract=="") {
//             toaster.pop('warning','提示','请输入用户协议号')
//             return;
//         }
//         commonFactory.ajax("/api/User/CancelAuthorization", { "":$scope.cancelstData.contract }, function (result) {
//             if(result.Code == 1){
//                 toaster.pop('success','成功','取消授权成功')
//             }else{
//                 toaster.pop('error','失败', result.ResposeData)
//             }
//         }, function () {
//             commonFactory.loading(false)
//         }, "post");
//     }
//     // 解绑卡
//     $scope.CancelBank = function(){
//         if ($scope.cancelstData.contract=="") {
//             toaster.pop('warning','提示','请输入用户协议号')
//             return;
//         }
//         commonFactory.loading(true)  
//         commonFactory.ajax("/api/User/CancelBank", { "":$scope.cancelstData.contract }, function (result) {
//             commonFactory.loading(false)  
//             if(result.Code == 1){
//                 toaster.pop('success','成功','解绑银行卡成功')
//             }else{
//                 toaster.pop('error','失败', result.ResposeData)
//             }
//         }, function () {
//             commonFactory.loading(false)
//         }, "post");
//     }
//     // 本地解绑卡
//     $scope.CancelLocalBank = function(){
//         if ($scope.cancelstData.contract=="") {
//             toaster.pop('warning','提示','请输入用户协议号')
//             return;
//         }
//         commonFactory.loading(true)  
//         commonFactory.ajax("/api/User/LocalCancelBank ", { "":$scope.cancelstData.contract }, function (result) {
//             commonFactory.loading(false)  
//             if(result.Code == 1){
//                 toaster.pop('success','成功','解绑本地银行卡成功')
//             }else{
//                 toaster.pop('error','失败', result.ResposeData)
//             }
//         }, function () {
//             commonFactory.loading(false)
//         }, "post");
//     }
//     // 签约订单号查询
//     $scope.QueryAuthorization = function(){
//         if ($scope.cancelstData.contract1=="") {
//             toaster.pop('warning','提示','请输入签约订单号')
//             return;
//         }
//         commonFactory.loading(true)  
//         commonFactory.ajax("/api/User/RealNameAuthenticationQuery", { orderNo:$scope.cancelstData.contract1 }, function (result) {
//             commonFactory.loading(false)  
//             if(result.Code == 1){
//                 $scope.realNameAuthentication = result.RealNameAuthenticationInfo
//             }else{
//                 toaster.pop('error','失败', result.ResposeData)
//             }
//         }, function () {
//             commonFactory.loading(false)
//         }, "post");
//     }
//     // 签约处理
//     $scope.TryBindContract = function(){
//         if (!$scope.realNameAuthentication) {
//             toaster.pop('warning','提示','请先输入签约订单号点击查询')
//             return;
//         }
//         var data={
//             Contracts: $scope.realNameAuthentication.Contracts, 
//             FullName: $scope.realNameAuthentication.UserName, 
//             IdCard: $scope.realNameAuthentication.UserIdentity, 
//             OrderNo: $scope.realNameAuthentication.OrderNo, 
//             UserId: $scope.realNameAuthentication.UserId
//         }
//         commonFactory.loading(true)  
//         commonFactory.ajax("/api/User/TryBindContract", data, function (result) {
//             commonFactory.loading(false)  
//             if(result.Code == 1){
//                 toaster.pop('success','成功','签约订单号处理成功')
//             }else{
//                 toaster.pop('error','失败', result.ResposeData)
//             }
//         }, function () {
//             commonFactory.loading(false)
//         }, "post");
//     }
//     // 活期退出修复
//     // $scope.currentExitData = {
//     //     ThisRedeemPirce:'',
//     //     FlexibleOrderId:'',
//     //     OnlyId:'',
//     //     SerialNo:''
//     // }
//     $scope.currentExitData = {
//         ExitId:''
//     }
//     var canclick = true;
//     $scope.currentExit = function(){
//         if(canclick){
//             canclick = false;
//             if (!$scope.currentExitData.ExitId) {
//                 toaster.pop('warning','提示','请输入退出记录ID')
//                 canclick = true;
//                 return;
//             }
//             // if (!$scope.currentExitData.ThisRedeemPirce) {
//             //     toaster.pop('warning','提示','请输入本次活期退出金额')
//             //     canclick = true;
//             //     return;
//             // }
//             // if (!$scope.currentExitData.FlexibleOrderId) {
//             //     toaster.pop('warning','提示','请输入活期订单')
//             //     canclick = true;
//             //     return;
//             // }
//             // if (!$scope.currentExitData.OnlyId) {
//             //     toaster.pop('warning','提示','请输入退出组Id')
//             //     canclick = true;
//             //     return;
//             // }
//             // if (!$scope.currentExitData.SerialNo) {
//             //     toaster.pop('warning','提示','请输入银行流水号')
//             //     canclick = true;
//             //     return;
//             // }
//             commonFactory.loading(true)  
//             commonFactory.ajax("/api/Flexible/TryExitProductOnBankFail", $scope.currentExitData , function (result) {
//                 commonFactory.loading(false)  
//                 canclick = true;
//                 if(result.Code == 1){
//                     toaster.pop('success','成功','活期退出修复成功')
//                 }else{
//                     toaster.pop('error','失败', result.ResposeData)
//                 }
//             }, function () {
//                 commonFactory.loading(false)
//             }, "post");
//         }else{
//             toaster.pop('warning','警告','活期退出修复进行中......')
//         }
//     }
// }]);
